package com.chd.calculator;

import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.orhanobut.logger.Logger;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    DecimalFormat df = new DecimalFormat("0.00");//double类型数据的格式化类，使double类型数据只输出两位数据

    @BindView(R.id.et_add_first) EditText etAddFirst;//控件绑定
    @BindView(R.id.et_add_second) EditText etAddSecond;
    @BindView(R.id.et_sub_first) EditText etSubFirst;
    @BindView(R.id.et_sub_second) EditText etSubSecond;
    @BindView(R.id.et_mult_first) EditText etMultFirst;
    @BindView(R.id.et_mult_second) EditText etMultSecond;
    @BindView(R.id.et_div_first) EditText etDivFirst;
    @BindView(R.id.et_div_second) EditText etDivSecond;

    @BindView(R.id.tv_add_rst) TextView tvAddRst;
    @BindView(R.id.tv_sub_rst) TextView tvSubRst;
    @BindView(R.id.tv_mult_rst) TextView tvMultRst;
    @BindView(R.id.tv_div_rst) TextView tvDivRst;

    //为按钮控件添加单击响应事件；调用所有计算
    @OnClick(R.id.btn_cal)void calAll(){
        calAdd();
        calSub();
        calMult();
        calDiv();
    }
    //加法
    @OnClick(R.id.btn_add_cal)void calAdd(){
        try {
            double firstNum = getNumFromEdit(etAddFirst);
            double secondNum = getNumFromEdit(etAddSecond);

            double rst = firstNum+secondNum;
            tvAddRst.setText(df.format(rst));
        }catch (Exception e) {
            tvAddRst.setText("输入错误");
            Logger.w(e.getMessage());
        }
    }

    @OnClick(R.id.btn_sub_cal)void calSub(){
        try {
            double firstNum = getNumFromEdit(etSubFirst);
            double secondNum = getNumFromEdit(etSubSecond);
            double rst = firstNum-secondNum;
            tvSubRst.setText(df.format(rst));
        }catch (Exception e) {
            tvSubRst.setText("输入错误");
            Logger.w(e.getMessage());
        }
    }
    @OnClick(R.id.btn_mult_cal)void calMult(){
        try {
            double firstNum = getNumFromEdit(etMultFirst);
            double secondNum = getNumFromEdit(etMultSecond);
            double rst = firstNum*secondNum;
            tvMultRst.setText(df.format(rst));
        }catch (Exception e) {
            tvMultRst.setText("输入错误");
            Logger.w(e.getMessage());
        }
    }
    @OnClick(R.id.btn_div_cal)void calDiv(){
        try {
            double firstNum = getNumFromEdit(etDivFirst);
            double secondNum = getNumFromEdit(etDivSecond);
            if (secondNum == 0.0){
                tvDivRst.setText("除数为0");
            }else {
                double rst = firstNum/secondNum;
                tvDivRst.setText(df.format(rst));
            }
        }catch (Exception e) {
            tvDivRst.setText("输入错误");
            Logger.w(e.getMessage());
        }
    }
//从编辑控件里获取数字
    private double getNumFromEdit(EditText et) throws Exception {
        String numStr = et.getText().toString().trim();
        double rst=0.0;
        try{
            rst = Double.valueOf(numStr);
        }catch (Exception e){
            throw new Exception(e.getMessage());
        }
        return rst;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//加载布局
        ButterKnife.bind(this);
    }
}
